<template>
  <div class="login-container">
    <!-- 背景图片 -->
    <div class="background-image"></div>
    
    <!-- 登录表单 -->
    <div class="login-form-container">
      <div class="login-form">
        <div class="form-header">
          <h1 class="title">欢迎回来</h1>
          <p class="subtitle">登录到 <span class="brand-name">学了吗</span></p>
        </div>
        
        <form @submit.prevent="handleLogin" class="form">
          <div class="form-group">
            <label for="account">账号</label>
            <input
              id="account"
              v-model="loginForm.account"
              type="text"
              placeholder="请输入用户名或邮箱"
              required
              class="form-input"
            />
          </div>
          
          <div class="form-group">
            <label for="password">密码</label>
            <div class="password-input-wrapper">
              <input
                id="password"
                v-model="loginForm.password"
                :type="showPassword ? 'text' : 'password'"
                placeholder="请输入密码"
                required
                class="form-input"
              />
              <button
                type="button"
                @click="showPassword = !showPassword"
                class="password-toggle"
              >
                {{ showPassword ? '👁️' : '👁️‍🗨️' }}
              </button>
            </div>
          </div>
          
          <div class="form-options">
            <label class="remember-me">
              <input type="checkbox" v-model="rememberMe" />
              <span>记住我</span>
            </label>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>
          
          <button type="submit" class="login-btn" :disabled="loading">
            <span v-if="loading">登录中...</span>
            <span v-else>登录</span>
          </button>
        </form>
        
        <div class="form-footer">
          <p>还没有账号？ <a href="#" @click="switchToRegister">立即注册</a></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '@/apis/access.js'
import { useUserStore } from '@/stores/userStore.js'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

// 响应式数据
const loginForm = reactive({
  account: '',
  password: ''
})

const showPassword = ref(false)
const rememberMe = ref(false)
const loading = ref(false)

// 页面加载时，检查是否有记住的账号和密码
onMounted(() => {
  const credentials = userStore.getRememberedCredentials()
  
  if (credentials.account) {
    loginForm.account = credentials.account
    if (credentials.password) {
      loginForm.password = credentials.password
    }
    rememberMe.value = true
  }
})

// 监听"记住我"状态变化
watch(rememberMe, (newVal) => {
  if (newVal) {
    // 如果勾选了记住我，立即保存当前账号和密码
    userStore.setRememberedCredentials(loginForm.account, loginForm.password)
  } else {
    // 如果取消勾选，清除记住的账号和密码
    userStore.clearRememberedCredentials()
  }
})

// 监听账号输入变化，如果勾选了"记住我"，自动保存
watch(() => loginForm.account, () => {
  if (rememberMe.value) {
    userStore.setRememberedCredentials(loginForm.account, loginForm.password)
  }
})

// 监听密码输入变化，如果勾选了"记住我"，自动保存
watch(() => loginForm.password, () => {
  if (rememberMe.value) {
    userStore.setRememberedCredentials(loginForm.account, loginForm.password)
  }
})

// 登录处理
const handleLogin = async () => {
  loading.value = true
  
  try {
     
    const response = await login(loginForm)
    
    if (response.code === 200) {
      // 使用store保存用户信息
      userStore.login(response.data.accessToken, response.data)
      
      // 根据"记住我"状态处理账号和密码保存
      if (rememberMe.value) {
        userStore.setRememberedCredentials(loginForm.account, loginForm.password)
      } else {
        userStore.clearRememberedCredentials()
      }
      
      // 登录成功后跳转
      router.push('/')
      ElMessage.success("登陆成功")
    } else {
      ElMessage.error(response.msg)
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}

// 切换到注册页面
const switchToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  margin: 0;
  padding: 0;
  z-index: 1000;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('@/assets/common/login-background.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  filter: brightness(0.7);
}

.login-form-container {
  position: relative;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: 20px;
}

.login-form {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  font-size: 2rem;
  font-weight: 700;
  color: #2d3748;
  margin: 0 0 8px 0;
}

.subtitle {
  color: #718096;
  font-size: 1rem;
  margin: 0;
}

.brand-name {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-weight: 700;
  font-size: 1.1rem;
  display: inline-block;
  text-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
}

.form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-weight: 600;
  color: #4a5568;
  font-size: 0.9rem;
}

.form-input {
  padding: 12px 16px;
  border: 2px solid #e2e8f0;
  border-radius: 10px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: white;
}

.form-input:focus {
  outline: none;
  border-color: #4299e1;
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
}

.password-input-wrapper {
  position: relative;
}

.password-toggle {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.2rem;
  padding: 4px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 10px 0;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: #4a5568;
  cursor: pointer;
}

.remember-me input[type='checkbox'] {
  width: 16px;
  height: 16px;
  cursor: pointer;
  position: relative;
  appearance: none;
  -webkit-appearance: none;
  border: 2px solid #cbd5e0;
  border-radius: 4px;
  background-color: white;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.remember-me input[type='checkbox']:checked {
  background-color: #4299e1;
  border-color: #4299e1;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Cpath fill='white' d='M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z'/%3E%3C/svg%3E");
  background-size: 12px;
  background-position: center;
  background-repeat: no-repeat;
}

.remember-me input[type='checkbox']:hover {
  border-color: #4299e1;
}

.forgot-password {
  color: #4299e1;
  text-decoration: none;
  font-size: 0.9rem;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #3182ce;
}

.login-btn {
  background: linear-gradient(135deg, #4299e1 0%, #3182ce 100%);
  color: white;
  border: none;
  padding: 14px;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(66, 153, 225, 0.3);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #e2e8f0;
}

.form-footer p {
  color: #718096;
  margin: 0;
}

.form-footer a {
  color: #4299e1;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.3s ease;
}

.form-footer a:hover {
  color: #3182ce;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-form {
    padding: 30px 20px;
    margin: 10px;
  }
  
  .title {
    font-size: 1.5rem;
  }
}
</style>